package book;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bookshopSM.BookshopGUI;
import bookshopSM.DrawPanel;

public class DeleteBook extends JPanel {
    private BookshopGUI userInterface1,userInterface2;
    private JButton  firstButton1,secondButton1, firstButton2,secondButton2;
    private JLabel label,ltitle;
    int vnoDelete;
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";
    String picturePath;
    private DrawPanel myPanel;

    public DeleteBook() throws SQLException{
        ltitle=new JLabel("删除图书信息               ");
        ltitle.setFont(new java.awt.Font("楷体",1,20));
        picturePath="bgp.jpg";
        myPanel=new DrawPanel();
        String names1[]={"请输入图书编号："};
        userInterface1 = new BookshopGUI(names1 );
        String names2[]={ "书     名","剩余库存","分      类","定 价(元)","促销价(元)","作     者", "出 版 社"};
        userInterface2 = new BookshopGUI(names2 );
        label=new JLabel("         图书封面           ");

        Box box = Box.createVerticalBox();
        Box boxinner=Box.createHorizontalBox();
        Box boxinnerin=Box.createVerticalBox();
        boxinnerin.add(myPanel);
        boxinnerin.add(label);
        boxinner.add(userInterface2);
        boxinner.add(boxinnerin);
        box.add(box.createVerticalStrut(15));
        box.add(ltitle);
        box.add(box.createVerticalStrut(15));
        box.add(userInterface1 );
        box.add(boxinner );
        this.add(box);
        setVisible( true );

        firstButton1 = userInterface1.getDoTask1Button();
        firstButton1.setText( "确认" );
        firstButton1.addActionListener(
                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        try {
                            DisplayRecord();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
        );

        secondButton1 = userInterface1.getDoTask2Button();
        secondButton1.setText( "清除" );
        secondButton1.addActionListener(

                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        userInterface1.clearFields();
                    }
                }
        );
        firstButton2 = userInterface2.getDoTask1Button();
        firstButton2.setText( "确认删除" );

        firstButton2.addActionListener(

                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        try {
                            deleteRecord();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }


                } // end anonymous inner class

        ); // end call to addActionListener

        secondButton2 = userInterface2.getDoTask2Button();
        secondButton2.setText( "清除" );
        secondButton2.addActionListener(

                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        userInterface2.clearFields();
                    }
                }
        );
        setSize( 500, 360 );
        setVisible( true );
    }

    private void DisplayRecord() throws SQLException {
        // TODO Auto-generated method stub
        conn = DriverManager.getConnection(url,username,password);
        Statement stmt=conn.createStatement();
        String fieldValues1[] = userInterface1.getFieldValues();
        vnoDelete=Integer.valueOf(fieldValues1[0]);
        try {
            System.out.print(vnoDelete);
            ResultSet rs = stmt.executeQuery("select * from Book where Bno="+vnoDelete);
            rs.next();//rs指向第一行之前
            // "书     名","剩余库存","分     类","定     价(元)","作     者", "出 版 社"
            userInterface2.fields[0].setText(rs.getString("Bname"));
            userInterface2.fields[1].setText(String.valueOf(rs.getInt("Bnum")));
            userInterface2.fields[2].setText(rs.getString("Bsort"));
            userInterface2.fields[3].setText(String.valueOf(rs.getFloat("Bprice")));
            userInterface2.fields[4].setText(String.valueOf(rs.getFloat("Bsellprice")));
            userInterface2.fields[5].setText(rs.getString("Bauthor"));
            userInterface2.fields[6].setText(rs.getString("Bpress"));
            picturePath=rs.getString("Bcover");
            myPanel.setimg(picturePath);
            myPanel.draw();
        }

        catch (Exception ee){
            JOptionPane.showMessageDialog(null, "无相关记录");
            System.out.println(ee);
        }
        finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn=null;
            }
        }

    }

    private void deleteRecord() throws SQLException {
        // TODO Auto-generated method stub
        conn = DriverManager.getConnection(url,username,password);
        Statement stmt=conn.createStatement();
        try {
            stmt.executeUpdate("delete from Book where Bno="+vnoDelete);
            JOptionPane.showMessageDialog(null, "删除成功");
        }

        catch (Exception ee){
            System.out.println(ee);
        }
        finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn=null;
            }
        }

    }


}
